/*
 * Created by JFormDesigner on Thu Jun 08 19:29:28 CST 2023
 */

package gui.admin;

import dao.ProductDao;
import dao.PurchaseDao;
import dao.PurchaseDetailsDao;
import dao.SaleDetailsDao;
import entity.*;
import gui.admin.tablemodel.PurchaseDetailsTableModel;
import gui.admin.tablemodel.PurchaseTableModel;
import gui.user.UserMainFrame;
import util.PurchaseDetailNoUtil;
import util.PurchaseNoUtil;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class AddPurchaseDetailFrame extends JFrame {
    ProductDao productDao = new ProductDao();
    public AddPurchaseDetailFrame(Purchase purchase) throws Exception {
        initComponents(purchase);
    }

    private void initComponents(Purchase purchase) throws Exception {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        productName = new JComboBox();
        purchaseNumber = new JTextField();
        addDetailBtn = new JButton();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        addPurchaseBtn = new JButton();
        cancelBtn = new JButton();

        List productList = productDao.getAllProducts();
        for (int i = 0; i < productList.size(); i++) {
            Product product = (Product) productList.get(i);
            productName.addItem(new Product(product.getProductId(),product.getProductName()){
                public String toString(){
                    String productName = product.getProductName();
                    return productName;
                }
            });
        }


        //======== this ========
        setTitle("\u6dfb\u52a0\u4f9b\u8d27\u5355");
        setPreferredSize(new Dimension(860, 520));
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label2 ----
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        label2.setText("\u5546\u54c1\u540d\u79f0\uff1a");
        contentPane.add(label2);
        label2.setBounds(35, 60, 115, 45);

        //---- label3 ----
        label3.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        label3.setText("\u8fdb\u8d27\u6570\u91cf\uff1a");
        contentPane.add(label3);
        label3.setBounds(35, 185, 115, 45);

        contentPane.add(productName);
        productName.setBounds(150, 60, 175, 50);
        contentPane.add(purchaseNumber);
        purchaseNumber.setBounds(150, 185, 175, 50);

        //---- addDetailBtn ----
        addDetailBtn.setText("\u52a0\u5165");
        addDetailBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(addDetailBtn);
        addDetailBtn.setBounds(30, 340, 95, 55);

        addDetailBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String purchaseDetailNo = PurchaseDetailNoUtil.randomOrderCode();//获取随机订单详情编号
                int index = productName.getSelectedIndex();//获取下拉框选择的索引
                Product product = (Product) productList.get(index);//根据索引获取商品对象
                String productId = product.getProductId();//获取商品编号
                int number = Integer.parseInt(purchaseNumber.getText());//获取商品数量输入框内容
                PurchaseDetails purchaseDetails = new PurchaseDetails(purchaseDetailNo,purchase.getPurchaseId(),productId,number);
                PurchaseDetailsDao purchaseDetailsDao = new PurchaseDetailsDao();
                try {
                    purchaseDetailsDao.addPurchaseDetail(purchaseDetails);
                    table1.setModel(new PurchaseDetailsTableModel(purchase.getPurchaseId()));//设置表格数据模型
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //======== scrollPane1 ========
        {
            scrollPane1.setViewportView(table1);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(370, 60, scrollPane1.getPreferredSize().width, 370);

        //---- addPurchaseBtn ----
        addPurchaseBtn.setText("完成");
        addPurchaseBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(addPurchaseBtn);
        addPurchaseBtn.setBounds(145, 340, 95, 55);

        addPurchaseBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                JOptionPane.showMessageDialog(null,"完成！");//弹出信息窗口
                dispose();
                try {
                    AdminMainFrame adminMainFrame = new AdminMainFrame();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //---- cancelBtn ----
        cancelBtn.setText("\u53d6\u6d88");
        cancelBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(cancelBtn);
        cancelBtn.setBounds(260, 340, 95, 55);
        //给取消按钮添加监听事件
        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                PurchaseDetailsDao purchaseDetailsDao = new PurchaseDetailsDao();
                try {
                    //根据入库单编号获取入库单下面的所有入库单明细
                    List purchaseDetailsList = purchaseDetailsDao.getAppointPurchaseDeatils(purchase.getPurchaseId());
                    //遍历每个查询出的入库单明细并删除
                    for (int i = 0; i < purchaseDetailsList.size(); i++) {
                        PurchaseDetails purchaseDetails = (PurchaseDetails) purchaseDetailsList.get(i);
                        purchaseDetailsDao.deletePurchaseDetails(purchaseDetails);
                    }
                    PurchaseDao purchaseDao = new PurchaseDao();
                    purchaseDao.deletePurchase(purchase);//调用删除方法
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
                JOptionPane.showMessageDialog(null,"此次供货单已删除！！");
                dispose();
                try {
                    AdminMainFrame adminMainFrame = new AdminMainFrame();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JComboBox productName;
    private JTextField purchaseNumber;
    private JButton addDetailBtn;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JButton addPurchaseBtn;
    private JButton cancelBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) throws Exception {
        AddPurchaseDetailFrame addPurchaseDetailFrame = new AddPurchaseDetailFrame(new Purchase());
    }
}
